package com.yunshipei.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.yunshipei.ui.activity.AlarmDialogActivity;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.NotifyInfo;


/**
 * Created by cike on 15-11-18.
 */
public class AlarmSeceiver extends BroadcastReceiver {
    private NotifyInfo ni = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("测试", "闹钟提醒相关操作");
        showNotification(context, intent);
    }

    /**
     * bundle.putSerializable("speech",speechRecognition);
     * intent.putExtra("speech",bundle);
     *
     * @param context
     * @param data
     */
    public void showNotification(Context context, Intent data) {
        Bundle speechBundle = data.getBundleExtra("speech1");
        ni = (NotifyInfo) speechBundle.getSerializable("speech");
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Log.d(AlarmSeceiver.class.getSimpleName(), "onPushMessageArrive-url:");
        Notification notification = null;
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (android.os.Build.VERSION.SDK_INT < 11) {
//            notification = new Notification(null, "自定义 notification", System.currentTimeMillis());
//            notification.setLatestEventInfo(context, "自定义 title", "有人去找你打球了!!!", pendingIntent);
//            notification.flags = Notification.FLAG_AUTO_CANCEL;
//            notification.defaults = Notification.DEFAULT_SOUND;
            Log.e("sdk version ", "sdk version < 11");
        } else {
            String msgContext = ni.getSpeechRecognition().getContent();
            System.out.println("提醒消息" + msgContext);
            notification = new Notification.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("消息提醒")
                    .setContentText(msgContext)
//                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL).build();
        }

        NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        nm.notify(0, notification);
        Intent intent2 = new Intent();
        intent2.setClass(context, AlarmDialogActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("notifiInfo", ni);
        intent2.putExtra("bundleObj", bundle);
        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent2);
    }


}
